package com.zds.passwordbox.fragment;

import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.zds.passwordbox.R;
import com.zds.passwordbox.common.Constant;
import com.zds.passwordbox.common.OnDialogDismissListener;
import com.zds.passwordbox.common.util.TOTPUtil;
import com.zds.passwordbox.data.dao.AccountDao;
import com.zds.passwordbox.data.dao.domain.UserAccount;

import java.util.ArrayList;
import java.util.List;

public class CodeAccountAdd extends BottomSheetDialogFragment {

    private final AccountDao accountDao;
    private OnDialogDismissListener onDialogDismissListener;

    private boolean isHidden = false;

    public CodeAccountAdd(AccountDao accountDao) {
       this.accountDao = accountDao;
    }

    // 设置接口的方法
    public void setOnDialogDismissListener(OnDialogDismissListener listener) {
        onDialogDismissListener = listener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        View view = super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.code_account_add, container, false);
        // 找到布局中的各个控件
        AutoCompleteTextView accountType = view.findViewById(R.id.autoCompleteTextView);
        EditText editTextUsername = view.findViewById(R.id.code_account_username);
        EditText editTextPassword = view.findViewById(R.id.code_account_password);
        Button buttonConfirm = view.findViewById(R.id.code_add_confirm);
        Button buttonCancel = view.findViewById(R.id.code_add_cancel);

        setLeftDrawable(accountType, ContextCompat.getDrawable(requireContext(), R.mipmap.type));
        setLeftDrawable(editTextUsername, ContextCompat.getDrawable(requireContext(), R.mipmap.default_account));
        setLeftDrawable(editTextPassword, ContextCompat.getDrawable(requireContext(), R.mipmap.password));

        ImageView showHidePassword = view.findViewById(R.id.code_show_hide);
        showHidePassword.setOnClickListener(v -> {
            isHidden = !isHidden;
            if (isHidden) {
                editTextPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                editTextPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
            editTextPassword.setSelection(editTextPassword.getText().length());
        });

        // 设置账户类型下拉选择框的选项数据
        List<String> accountTypeList = new ArrayList<>(Constant.codeAccountType);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(requireContext(), android.R.layout.simple_dropdown_item_1line, accountTypeList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        accountType.setAdapter(adapter);
        accountType.setThreshold(1);
        // 确认按钮点击事件处理
        buttonConfirm.setOnClickListener(v -> {
            String accountTypeText = accountType.getText().toString();
            String username = editTextUsername.getText().toString();
            String password = editTextPassword.getText().toString();
            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(requireContext(), "用户名和密钥不能为空", Toast.LENGTH_SHORT).show();
            } else {
                boolean validBase32 = TOTPUtil.isValidBase32(password);
                if (!validBase32) {
                    Toast.makeText(requireContext(), "密钥不合法！", Toast.LENGTH_SHORT).show();
                    return;
                }
                UserAccount account = new UserAccount(accountTypeText, username, password, 1);
                accountDao.encryptedInsert(account);
                editTextUsername.setText(null);
                editTextPassword.setText(null);
                dismiss();
            }
        });
        // 取消按钮点击事件处理
        buttonCancel.setOnClickListener(v -> {
            editTextUsername.setText(null);
            editTextPassword.setText(null);
            dismiss();
        });
        return view;
    }

    private void setLeftDrawable(EditText editText, Drawable drawable) {
        if (drawable == null) return;
        drawable.setBounds(0, 0, 70, 70);
        editText.setCompoundDrawables(drawable, null, null, null);
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        if (onDialogDismissListener!= null) {
            onDialogDismissListener.onDialogDismiss();
        }
    }
}
